package com.edu.framework.websocket.config;

import com.edu.framework.websocket.handler.WebSocketHandler;
import com.edu.framework.websocket.interceptor.WebSocketInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * websocket 配置
 *
 * @author xxxx
 */

@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {


    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler(),"/ws")
                .addInterceptors(webSocketInterceptor())
                .setAllowedOrigins("*");
    }

    @Bean
    public WebSocketHandler webSocketHandler(){
       return new WebSocketHandler(applicationContext);
    }

    @Bean
    public WebSocketInterceptor webSocketInterceptor(){
       return new WebSocketInterceptor();
    }

}
